package org.com.system.util.sql;

import java.lang.reflect.Field;

import org.com.system.util.mate.SQLMate;
import org.com.system.util.model.ActionValues;
import org.com.system.util.model.Model;
import org.com.system.util.model.Page;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * 对Criteria进行加工处理
 * @author Gandilong at 2012-9-18下午01:05:51
 */
public class CriteriaGener {

	public static void process(Criteria criter,ActionValues<String, Object> values, Class<?> entityClass){
		Field[] fields=entityClass.getDeclaredFields();
		try{
		    if(null!=fields&&fields.length>0){
		        for(Field field:fields){
		    	    if(field.isAnnotationPresent(SQLMate.class)){
			    	    SQLMate mate=field.getAnnotation(SQLMate.class);
			    	    addRestriction(field.getName(),mate,values,criter);
		 	        }else{
		 	    	    addRestriction(field.getName(),null,values,criter);
		 	        }
		        }
		    }
		    addOder(criter,values,entityClass);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加条件限制
	 * @param field
	 * @param mate
	 * @param values
	 * @param criter
	 */
	public static void addRestriction(String field,SQLMate mate,ActionValues<String, Object> values,Criteria criter){
		if(null!=mate){
				if(org.com.system.util.mate.Link.EQUALS.equals(mate.link())){
					//相等匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.eq(field, values.get(field)));
					}
					
				}else if(org.com.system.util.mate.Link.GREAT.equals(mate.link())){
					//大于匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.gt(field, values.get(field)));
					}
					
				}else if(org.com.system.util.mate.Link.GREAT_EQUALS.equals(mate.link())){
					//大于等于匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.ge(field, values.get(field)));
					}
					
				}else if(org.com.system.util.mate.Link.LESS.equals(mate.link())){
					//小于匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.lt(field, values.get(field)));
					}
					
				}else if(org.com.system.util.mate.Link.LESS_EQUALS.equals(mate.link())){
					//小于等于匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.le(field, values.get(field)));
					}
					
				}else if(org.com.system.util.mate.Link.LIKE.equals(mate.link())){
					//相似匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.like(field, "%"+values.get(field)+"%"));
					}
					
				}else if(org.com.system.util.mate.Link.OTHER.equals(mate.link())){
					//其它属性字段相等匹配
					if(!values.isEmpty(field)){
					    criter.add(Restrictions.eqProperty(field,String.valueOf(values.get(field))));
					}
				}
		}else{
			if(!values.isEmpty(field)){
			    criter.add(Restrictions.eq(field, values.get(field)));
			}
		}
	}
	
	/**
	 * 排序实现
	 * @param entityCalss
	 * @param criter
	 * @param values
	 */
	public static void addOder(Criteria criter,ActionValues<String, Object> values,Class<?> entityClass){
		 if(null!=values.get("page")){
			 //1.可以在ActionValues里放一个Page对象
		     Page page=(Page)values.get("page");
		     if("asc".equalsIgnoreCase(page.getOrder())){
		         criter.addOrder(Order.asc(page.getOrderby()));
		     }else{
		         criter.addOrder(Order.desc(page.getOrderby()));
		     }
		 }else if(null!=values.get("orderBy")){
			 //2.可以在ActionValues里放一个或两个排序属性
		     if("asc".equalsIgnoreCase(values.getString("order"))){
		         criter.addOrder(Order.asc(values.getString("orderBy")));
		     }else{
		         criter.addOrder(Order.desc(values.getString("orderBy")));
		     }
		 }else{
			 //3.也可以在实体类中指定排序列
			 boolean isOrderd=false;
			 Field[] fields=entityClass.getDeclaredFields();
			 if(null!=fields&&fields.length>0){
				 for(Field field:fields){
					 try{
						 if(field.isAnnotationPresent(org.com.system.util.mate.Order.class)){
							 isOrderd=true;
							 org.com.system.util.mate.Order order=field.getAnnotation(org.com.system.util.mate.Order.class);
							 if("asc".equalsIgnoreCase(order.value())){
						    	 criter.addOrder(Order.asc(field.getName()));
						     }else{
						    	 criter.addOrder(Order.desc(field.getName()));
						     }
						 }
					 }catch(Exception e){
						 e.printStackTrace();
					 }
				 }
			 }
			 //如果表中未注解用那个字段来排序，刚用Model类中的默认字段
			 if(!isOrderd){
				 fields=Model.class.getDeclaredFields();
				 for(Field _field:fields){
					 try{
						 if(_field.isAnnotationPresent(org.com.system.util.mate.Order.class)){
							 org.com.system.util.mate.Order order=_field.getAnnotation(org.com.system.util.mate.Order.class);
							 if("asc".equalsIgnoreCase(order.value())){
						    	 criter.addOrder(Order.asc(_field.getName()));
						     }else{
						    	 criter.addOrder(Order.desc(_field.getName()));
						     }
						 }
					 }catch(Exception e){
						 e.printStackTrace();
					 }
				 }
			 }
		 }
	}
}
